BiocManager::install("maftools")
library(TCGAbiolinks)
# 查看TCGA中33种癌症的简称
setwd("../突变瀑布图/")
library(ComplexHeatmap)
library(maftools)
projects <- TCGAbiolinks::getGDCprojects()$project_id ##获取癌症名字
projects <- projects[grepl('^TCGA', projects, perl=TRUE)]

projects
##[1] "TCGA-BRCA" "TCGA-THCA" "TCGA-UCEC" "TCGA-DLBC" "TCGA-COAD"
##[6] "TCGA-CESC" "TCGA-BLCA" "TCGA-CHOL" "TCGA-ESCA" "TCGA-ACC" 
##[11] "TCGA-KICH" "TCGA-HNSC" "TCGA-LIHC" "TCGA-MESO" "TCGA-LAML"
##[16] "TCGA-KIRP" "TCGA-KIRC" "TCGA-GBM"  "TCGA-LGG"  "TCGA-SARC"
##[21] "TCGA-PCPG" "TCGA-READ" "TCGA-PAAD" "TCGA-LUAD" "TCGA-PRAD"
##[26] "TCGA-OV"   "TCGA-LUSC" "TCGA-TGCT" "TCGA-THYM" "TCGA-UVM" 
##[31] "TCGA-SKCM" "TCGA-UCS"  "TCGA-STAD"
query <- GDCquery(
  project = c("TCGA-LGG", "TCGA-GBM"), 
  data.category = "Simple Nucleotide Variation", 
  access = "open", 
  data.type = "Masked Somatic Mutation"
)
GDCdownload(query)
maf <- GDCprepare(query)
dim(maf)
laml.maf = maf 
save.image(file = "LGG_GBM.RData")
maf$long_Barcode = maf$Tumor_Sample_Barcode 
maf$Tumor_Sample_Barcode = substr(maf$Tumor_Sample_Barcode,1,12)
length(unique(maf$Tumor_Sample_Barcode))

#path to TCGA LAML MAF file

laml.maf = maf 
#clinical information containing survival information and histology. This is optional
cliFile="high"

cli1=read.table(cliFile, header=T, sep="\t", check.names=F)
cli1$sample <- gsub("\\.","-",cli1$sample)

colnames(cli1)[colnames(cli1) == "sample"] <- "Tumor_Sample_Barcode"
colnames(cli1)[colnames(cli1) == "TCGA.OS.time"] <- "clinical_stage"
colnames(cli1)[colnames(cli1) == "TCGA.OS"] <- "OS"
cli1$Tumor_Sample_Barcode <- substr(cli1[, 1], 1, 12)
laml_matched <- laml.maf[laml.maf$Tumor_Sample_Barcode %in% cli1$Tumor_Sample_Barcode, ]
laml_cli1 <- cli1[cli1$Tumor_Sample_Barcode %in% laml.maf$Tumor_Sample_Barcode, ]
laml = read.maf(maf = laml_matched, clinicalData = laml_cli1)


#Typing laml shows basic summary of MAF file.
laml
getSampleSummary(laml)
#Shows gene summary.
getGeneSummary(laml)
#shows clinical data associated with samples
getClinicalData(laml)
#Shows all fields in MAF
getFields(laml)
#Writes maf summary to an output file with basename laml.
write.mafSummary(maf = laml, basename = 'laml')
plotmafSummary(maf = laml, rmOutlier = TRUE, addStat = 'median', dashboard = TRUE, titvRaw = FALSE)
oncoplot(maf = laml, top = 10)

maftools::oncoplot(laml, 
                   top = 30,
                   writeMatrix = T)
##会生成一个onco_matrix.txt，默认为ComplexHeatmap需要的格式
coad_mat <- read.table(file = "onco_matrix.txt",
                       header=T,sep="\t",check.names = F
)
coad_mat[1:4,1:4]
# 删除第 241 到 250 列
coad_mat <- coad_mat[, -c(410:416)]
coad_mat <- coad_mat[, -1]
col = c("Missense_Mutation" = "#00A664", "Nonsense_Mutation" = "#A65628", 
        "Splice_Site" = "#BEBADA","Frame_Shift_Del"="#FB8072",
        "Frame_Shift_Ins"="#4DAF4A","In_Frame_Del"="#E41A1C",
        "In_Frame_Ins"="#FF7F00","Nonstop_Mutation"="#FCCDE5",
        "Multi_Hit"="#8DA0CB", "Translation_Start_Site" = "#BEBADA"
)

alter_fun = list(
  background = alter_graphic("rect", fill = "grey90"),
  Missense_Mutation = alter_graphic("rect", fill = col["Missense_Mutation"]),
  Nonsense_Mutation = alter_graphic("rect", fill = col["Nonsense_Mutation"]),
  Splice_Site = alter_graphic("rect", height = 0.33, fill = col["Splice_Site"]),
  Frame_Shift_Del = alter_graphic("rect", fill = col["Frame_Shift_Del"]),
  Frame_Shift_Ins = alter_graphic("rect", fill = col["Frame_Shift_Ins"]),
  In_Frame_Del = alter_graphic("rect", fill = col["In_Frame_Del"]),
  In_Frame_Ins = alter_graphic("rect", fill = col["In_Frame_Ins"]),
  Nonstop_Mutation = alter_graphic("rect", fill = col["Nonstop_Mutation"]),
  Multi_Hit = alter_graphic("rect", fill = col["Multi_Hit"]),
  Translation_Start_Site = alter_graphic("rect", fill = col["Translation_Start_Site"])
)

heatmap_legend_param = list(title = "Alternations", 
                            at = c("Missense_Mutation", "Nonsense_Mutation", 
                                   "Splice_Site","Frame_Shift_Del",
                                   "Frame_Shift_Ins","In_Frame_Del",
                                   "In_Frame_Ins","Nonstop_Mutation",
                                   "Multi_Hit", "Translation_Start_Site"
                            ), 
                            labels = c("Missense_Mutation", "Nonsense_Mutation", 
                                       "Splice_Site","Frame_Shift_Del",
                                       "Frame_Shift_Ins","In_Frame_Del",
                                       "In_Frame_Ins","Nonstop_Mutation",
                                       "Multi_Hit", "Translation_Start_Site"
                            )
)
##临床信息
cliFile="clinial_all.txt"
cliall <- read.table(cliFile, header=T, sep="\t", check.names=F)
cliall[,1] <- substr(cliall[,1], 1, 12)
dup_rows <- cliall[duplicated(cliall[, 1]), 1]
dup_suffix <- ave(dup_rows, dup_rows, FUN = seq_along)
cliall[duplicated(cliall[, 1]), 1] <- paste0(dup_rows, ".", dup_suffix)
rownames(cliall) <- cliall[,1]
comgene <- intersect(colnames(coad_mat),rownames(cliall))
cliall1 <- cliall[comgene,] 
colnames(cliall)
columnanno <- HeatmapAnnotation(stage=cliall1$paper_Grade,
                                gender=cliall1$gender,
                                race=cliall1$race,
                                MGMT.promoter.status=cliall1$paper_MGMT.promoter.status,
                                show_annotation_name = T,
                                col = list(
                                  stage = c("G2" = "#DCE125", "G3" = "#5086C4", "G4" = "#B24745"),
                                  gender = c("male" = "#A5C2E2", "female" = "#FF6100"),
                                  MGMT.promoter.status =c("Unmethylated" = "#6B7EB9", "Methylated" = "#72C3A3")
                                )
                                
)
oncoPrint(coad_mat,
          alter_fun = alter_fun,
          col = col, 
          heatmap_legend_param = heatmap_legend_param,
          remove_empty_columns = T, 
          remove_empty_rows = T,
          bottom_annotation = columnanno
)

